from pathlib import Path

import yaml


def write_yaml(path, data):
    """
    将数据转写入yaml
    :return: yaml type
    """
    if not Path(path).exists():
        Path(path).touch()
    with open(path, 'a+', encoding='utf-8') as f:
        yaml.safe_dump(data, f, allow_unicode=True)


def write_yaml_template():
    """
    统一的yaml模板
    :return:
    """
    yaml_template = [{
        'feature': None, 'story': None, 'title': None, 'request': {
            'method': None,
            'url': None,
            'headers': None,
            'params': None,
        },
        'validate': None
    }]
    with open('template.yaml', 'w', encoding='utf-8') as f:
        yaml.safe_dump(yaml_template, f, allow_unicode=True, sort_keys=False)


def read_yaml_all(path):
    with open(path, 'r', encoding='utf-8') as f:
        value = yaml.safe_load(f)
        return value


def read_yaml_by_key(path, key):
    """
    读取yaml中的信息
    :return: dict type
    """
    with open(path, 'r', encoding='utf-8') as f:
        value = yaml.safe_load(f)
        assert type(key) == type(value), '数据类型不一致,yaml为列表类型[]'
        return value[key]


def dump_yaml(path, data):
    """
    将字典转写入yaml
    :return: yaml type
    """
    with open(path, 'w', encoding='utf-8') as f:
        yaml.safe_dump(data, f, allow_unicode=True)


def clean_yaml(path):
    """

    :param path:
    :return:
    """
    with open(path, 'w', encoding='utf-8') as f:
        pass
